<html>

<head>

</head>

<body>

    <ul id="ul">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>

    <script>

        let ul = document.querySelector('#ul');
        ul.addEventListener('click', event => {
            console.log(event);
        });

        const arr = [1, 2, 3];
        console.log(Array);
        console.log(Array.isArray(arr));
        console.log(arr.__proto__ === Array.prototype);
        console.log(arr.constructor === Array);
        console.log(arr instanceof Array)



        // function test() {
        //     let a = 1;
        //     while (true) {
        //         console.log(a);
        //         if (a > 10) return false;
        //         a++;
        //     }
        // }
        // test();

        console.log(0.1 + 0.2, (0.1 + 0.2).toFixed(2))

        function test111() {
            console.log(arguments)
        }
        var o1 = new test111(1, 2, 3, 4);

        function objectFactory() {
            let newObject = null;
            let constructor = Array.prototype.shift.call(arguments);
            let result = null;
            // 判断参数是否是一个函数
            if (typeof constructor !== "function") {
                console.error("type error");
                return;
            }
            // 新建一个空对象，对象的原型为构造函数的 prototype 对象
            newObject = Object.create(constructor.prototype);
            // 将 this 指向新建对象，并执行函数
            result = constructor.apply(newObject, arguments);
            // 判断返回对象
            let flag = result && (typeof result === "object" || typeof result === "function");
            // 判断返回结果
            return flag ? result : newObject;
        }
        // 使用方法
        var o2 = objectFactory(test111, 5, 7, 8, 9);
        console.log(o1.constructor == o2.contructor);

        // for (var i = 1; i <= 5; i++) {
        //     setTimeout(function timer() {
        //         console.log(i)
        //     }, i * 1000)

        //     (function (j) {
        //         setTimeout(function timer() {
        //             console.log(j)
        //         }, j * 1000)
        //     })(i)
        // }
        for (var i = 1; i <= 5; i++) {
            setTimeout(function timer(j) {
                console.log(j)
            }, i * 1000, i)
        }

        function* generatorForLoop(num) {
            for (let i = 0; i < num; i += 1) {
                yield console.log(i, "generator");
            }
        }

        const genForLoop = generatorForLoop(5);

        genForLoop.next(); // 首先 console.log —— 0
        genForLoop.next(); // 1
        genForLoop.next(); // 2
        genForLoop.next(); // 3
        genForLoop.next(); // 4


        const promise1 = new Promise(function (resolve, reject) {
            resolve({ success: "成功" })
            // reject({ error: "失败" });
        });
        const promise2 = new Promise(function (resolve, reject) {
            resolve({ success: "成功+++1" })
            // reject({ error: "失败+++1" });
        });
        const promise3 = new Promise(function (resolve, reject) {
            // resolve({ success: "成功~~~~~1" })
            reject({ error: "失败~~~~~1" });
        });
        Promise.all([promise1, promise2, promise3]).then(res => {
            console.log(res, "all");
            //结果为：[1,2,3] 
        }, function (error) { //这个和catch都有的时候执行这个函数
            console.log(error, "function", "all")
        }).catch((error) => {
            console.log(error, "all")
        })

        Promise.race([promise1, promise2, promise3]).then(res => {
            console.log(res, "race");
        }, rej => {
            console.log(rej, "race")
        })


        promise1.finally()


        function testAsy(x) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    reject(x);
                }, 3000)
            }
            )
        }
        async function testAwt() {
            let result = await testAsy('hello world');
            console.log(result);    // 3秒钟之后出现hello world
            console.log('cuger')   // 3秒钟之后出现cug
        }
        testAwt();
        console.log('cug')  //立即输出cug


        let onWatch = (obj, setBind, getLogger) => {
            let handler = {
                get(target, property, receiver) {
                    getLogger(target, property)
                    return Reflect.get(target, property, receiver)
                },
                set(target, property, value, receiver) {
                    setBind(value, property)
                    return Reflect.set(target, property, value)
                }
            }
            return new Proxy(obj, handler)
        }
        let obj = { a: 1 }
        let p = onWatch(
            obj,
            (v, property) => {
                console.log(`监听到属性${property}改变为${v}`)
            },
            (target, property) => {
                console.log(`'${property}' = ${target[property]}`)
            }
        )
        p.a = 2 // 监听到属性a改变
        p.a // 'a' = 2


        const watchProxy = (obj) => {
            return new Proxy(obj, {
                get(target, property, receiver) {
                    console.log(target, property, receiver, "get-watch");
                    return Reflect.get(target, property, receiver);
                },
                set(target, property, value, receiver) {
                    console.log(target, property, value, receiver, "set-watch");
                    return Reflect.set(target, property, value, receiver);
                }
            })
        }

        let p1 = watchProxy(obj);
        p1.a = "重新测试"
        console.log(p1, p1.a);

        class LoginForm {
            constructor() {
                this.state = 'hide'
            }
            show() {
                if (this.state === 'show') {
                    alert('已经显示')
                    return
                }
                this.state = 'show'
                console.log('登录框显示成功', this)
            }
            hide() {
                if (this.state === 'hide') {
                    alert('已经隐藏')
                    return
                }
                this.state = 'hide'
                console.log('登录框隐藏成功', this)
            }
        }
        LoginForm.getInstance = (function () {
            let instance
            return function () {
                if (!instance) {
                    instance = new LoginForm()
                }
                return instance
            }
        })()
        let obj1 = LoginForm.getInstance()
        obj1.show()
        let obj2 = LoginForm.getInstance()
        obj2.hide()
        console.log(obj1 === obj2)





    </script>
</body>

</html>